/*
 * @Author: liu.lin liu.lin@hzleaper.com
 * @Date: 2024-04-09 17:00:33
 * @LastEditors: liu.lin liu.lin@hzleaper.com
 * @LastEditTime: 2024-07-15 10:38:18
 * @FilePath: \nest_temp\src\obtain-system\obtain-system.service.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { Injectable, Logger } from '@nestjs/common';
import { CreateObtainSystemDto } from './dto/create-obtain-system.dto';
import { UpdateObtainSystemDto } from './dto/update-obtain-system.dto';
import * as path from 'path';
import * as fs from 'fs';
import * as os from 'os';
// import diskinfo from 'diskinfo'
import * as diskinfo from 'diskinfo';

@Injectable()
export class ObtainSystemService {
  constructor(private readonly logger: Logger) {}
  create(createObtainSystemDto: CreateObtainSystemDto) {
    return 'This action adds a new obtainSystem';
  }

  findAll() {
    // 获取系统桌面路径
    // 获取系统桌面路径
    const desktopPath = path.join(
      process.env.HOME || process.env.USERPROFILE,
      'Desktop',
    );
    this.logger.log('log 类型日志');
    this.logger.warn('warn 类型日志');
    this.logger.error('error 类型日志');
    // console.log(`desktopPath`, desktopPath);
    const data = [];
    diskinfo.getDrives(function (err, aDrives) {
      for (var i = 0; i < aDrives.length; i++) {
        console.log('Drive ' + aDrives[i].filesystem);
        console.log('blocks ' + aDrives[i].blocks);
        console.log('used ' + aDrives[i].used);
        console.log('available ' + aDrives[i].available);
        console.log('capacity ' + aDrives[i].capacity);
        console.log('mounted ' + aDrives[i].mounted);
        if (aDrives[i].blocks !== 0) {
          data.push(aDrives[i]);
        }

        console.log('-----------------------------------------');
      }
    });
    // 获取系统所有磁盘的信息
    // 使用函数获取系统桌面的磁盘信息
    getDirectoryInfo(desktopPath)
      .then((info) => {
        console.log('Desktop directory info:', info);
      })
      .catch((error) => {
        console.error('Error getting directory info:', error);
      });

    // const directoryPath = path.join(__dirname, 'path/to/directory'); // 替换为你的目录路径

    // fs.readdir(directoryPath, (err, files) => {
    //   if (err) {
    //     return console.log('Unable to scan directory: ' + err);
    //   }
    //   // 文件名在files数组中
    //   files.forEach((file) => {
    //     console.log(file, '11111111');
    //   });
    // });

    // const desktopPath = getDesktopPath();
    // console.log('Desktop Path:', desktopPath);
    return {
      data,
    };
  }
}

const getDesktopPath = () => {
  const platform = os.platform();
  let desktopPath = '';

  switch (platform) {
    case 'win32':
      desktopPath = path.join(os.homedir(), 'Desktop');
      break;
    case 'darwin':
      desktopPath = path.join(os.homedir(), 'Desktop');
      break;
    case 'linux':
      desktopPath = path.join(os.homedir(), 'Desktop');
      break;
    default:
      throw new Error('Unsupported platform');
  }

  return desktopPath;
};

// 定义一个函数来获取指定目录的磁盘信息
function getDirectoryInfo(directoryPath: string): Promise<{
  blocks: number;
  used: number;
  available: number;
  capacity: string;
  mounted: string;
}> {
  return new Promise((resolve, reject) => {
    fs.stat(directoryPath, (err, stats) => {
      if (err) {
        reject(err);
      } else {
        const blocks = stats.blocks;
        const used = stats.size;
        const available = stats.size;
        const capacity = ((stats.size / stats.blocks) * 100).toFixed(2) + '%';
        const mounted = directoryPath;

        resolve({ blocks, used, available, capacity, mounted });
      }
    });
  });
}
